using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Sheep_Breeding.Domain.production_a;
using Sheep_Breeding.ErrorCode;
using Sheep_Breeding.Read.Api.Dtos;
using Sheep_Breeding.Read.Api.OperationServices.IServices;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Sheep_Breeding.Read.Api.Controllers
{
    /// <summary>
    /// 母羊配种查询控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class EweMatingController : ControllerBase
    {
        private readonly IEweMatingServices _eweMatingServices;
        private readonly ILogger<EweMatingController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="eweMatingServices">母羊配种查询服务</param>
        /// <param name="logger">日志记录器</param>
        public EweMatingController(
            IEweMatingServices eweMatingServices,
            ILogger<EweMatingController> logger)
        {
            _eweMatingServices = eweMatingServices ?? throw new ArgumentNullException(nameof(eweMatingServices));
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        }

        /// <summary>
        /// 获取母羊配种列表
        /// </summary>
        /// <param name="queryDto">查询条件</param>
        /// <returns>母羊配种列表</returns>
        [HttpGet("list")]
        public async Task<ApiResult<ApiPaging<List<EweMatingListDto>>>> GetEweMatingList([FromQuery] QueryEweMatingDto queryDto)
        {
            if (queryDto == null)
            {
                return ApiResult<ApiPaging<List<EweMatingListDto>>>.Err("查询条件不能为空");
            }
            
            try
            {
                _logger.LogInformation("开始查询母羊配种列表");
                var result = await _eweMatingServices.GetEweMatingListAsync(queryDto);
                
                if (result?.msg != null)
                {
                    _logger.LogInformation($"查询母羊配种列表完成：{result.msg}");
                }
                
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError($"查询母羊配种列表异常：{ex.Message}");
                return ApiResult<ApiPaging<List<EweMatingListDto>>>.Err($"查询异常：{ex.Message}");
            }
        }

        /// <summary>
        /// 根据配种编号获取母羊配种详情
        /// </summary>
        /// <param name="breedingId">配种编号</param>
        /// <returns>母羊配种详情</returns>
        [HttpGet("byId")]
        // [Authorize] // 临时注释掉授权要求，用于测试
        public async Task<ApiResult<Sheep_Breeding.Domain.production_a.Ewe_mating>> GetEweMatingById([FromQuery] string breedingId)
        {
            try
            {
                _logger.LogInformation($"开始查询母羊配种详情，配种编号：{breedingId}");
                
                var result = await _eweMatingServices.GetEweMatingById(breedingId);
                
                _logger.LogInformation($"查询母羊配种详情完成，配种编号：{breedingId}，结果：{result.msg}");
                
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError($"查询母羊配种详情异常：{ex.Message}，配种编号：{breedingId}");
                throw;
            }
        }

        /// <summary>
        /// 根据耳号获取母羊配种记录
        /// </summary>
        /// <param name="earNumber">耳号</param>
        /// <returns>母羊配种记录</returns>
        [HttpGet("byEarNumber")]
        // [Authorize] // 临时注释掉授权要求，用于测试
        public async Task<ApiResult<Sheep_Breeding.Domain.production_a.Ewe_mating>> GetEweMatingByEarNumber([FromQuery] string earNumber)
        {
            try
            {
                _logger.LogInformation($"开始根据耳号查询母羊配种记录，耳号：{earNumber}");
                
                var result = await _eweMatingServices.GetEweMatingByEarNumber(earNumber);
                
                _logger.LogInformation($"根据耳号查询母羊配种记录完成，耳号：{earNumber}，结果：{result.msg}");
                
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError($"根据耳号查询母羊配种记录异常：{ex.Message}，耳号：{earNumber}");
                throw;
            }
        }

        /// <summary>
        /// 主表+明细表联合分页查询（明细只取最新一条）
        /// </summary>
        [HttpPost("withDetail")]
        public async Task<ApiResult<ApiPaging<List<EweMatingWithDetailDto>>>> GetEweMatingWithDetailList([FromQuery] SearchEweMatingDto searchDto)
        {
            return await _eweMatingServices.GetEweMatingWithDetailList(searchDto);
        }
    }
} 